// 成绩

const achievement = require("../../model/examination/achievement");
// 验证数据功能模块
const { checkNull, isJson } = require("../../rules/verification");
// 获取当前时间
const { getTime } = require("../../utils/gettime")
const testpaper = require("../../model/examination/testpaper");
const question = require("../../model/question/question")

const score = async (testpaperId, achiObj) => {
    // try{ 
    // 根据试卷id查询试卷
    let res = await testpaper.findOne({ where: { id: testpaperId } })
    let data = {}
    if (res.choice) data.choice = JSON.parse(res.choice)
    if (res.answer) data.answer = JSON.parse(res.answer)
    if (res.conpletion) data.conpletion = JSON.parse(res.conpletion)
    if (res.judge) data.judge = JSON.parse(res.judge)
    // return data
    if (data.choice) {
        let choice = await question.findAll({ where: { id: data.choice },attributes:["answer"] })
        choice.forEach(item => {
            item.answer = JSON.parse(item.answer)
            console.log(item);
        })
        return choice
    }
    // }catch(err){return { msg: "评分失败1", code: "400"}}
}



// 根据id获取成绩(检查试卷)
// 实现自动判卷 主要审批选择和判断题 并返回分数 
exports.getById = async ({ id }) => {
    try {
        let checkedId = checkNull(id, "成绩id")
        if (checkedId.code != "200") return checkedId
        let res = await achievement.findOne({ where: { id: checkedId.data } })
        res.choice = JSON.parse(res.choice)
        let achiObj = {}
        if (res.choice) achiObj.choice = res.choice
        if (res.answer) achiObj.answer = res.answer
        if (res.conpletion) achiObj.conpletion = res.conpletion
        if (res.judge) achiObj.judge = res.judge
        let scoreRes = score(res.testpaperId, achiObj)

        return scoreRes
        if (res) return { msg: "获取成绩成功", code: "200", data: res }
        return { msg: "获取成绩失败1", code: "400" }
    } catch (err) { return { msg: "获取成绩失败2", code: "400" } }
}
// 获取成绩列表
exports.getList = async ({ id }) => {
    try {
    } catch (err) { return { msg: "成绩失败", code: "400" } }
}

// let aaa = [{"index": "1","value": "A"},{"index": "1","value": "A"},{"index": "1","value": "A"}]

// [{"a":"a"},{"a":"a"}]

[{ "index": "22", "answer": ["b"] }]

// 添加成绩
exports.create = async ({ name, choice, answer, judge, conpletion, testpaper_id, testnumber }) => {
    try {
        let checkedName = checkNull(name, "考生姓名")
        if (checkedName.code !== "200") return checkedName
        let checkedId = checkNull(testpaper_id, "试卷id")
        if (checkedId.code !== "200") return checkedId
        let checkedTest = checkNull(testnumber, "考生考号")
        if (checkedTest.code !== "200") return checkedTest
        let questionArr = [
            { name: choice, title: "选择题答案", key: "choiceObj" },
            { name: answer, title: "简答题答案", key: "answerObj" },
            { name: judge, title: "判断题答案", key: "judgeObj" },
            { name: conpletion, title: "填空题答案", key: "conpletionObj" }
        ]
        let choiceObj = []
        let answerObj = []
        let judgeObj = []
        let conpletionObj = []
        // 判断答案是否为JSON格式
        try {
            questionArr.forEach(item => {
                if (item.name) {
                    let checkedItem = isJson(item.name, item.title)
                    if (checkedItem.code !== "200") throw new Error(JSON.stringify(checkedItem))
                    switch (item.key) {
                        case "choiceObj": choiceObj = checkedItem.data; break;
                        case "answerObj": answerObj = checkedItem.data; break;
                        case "judgeObj": judgeObj = checkedItem.data; break;
                        case "conpletionObj": conpletionObj = checkedItem.data; break;
                    }
                }
            })
        } catch (e) {
            let str = e.toString();
            if (str.length > 7) {
                str = str.substr(7);
            } else {
                str = `{"msg":"未知错误","code":"400"}`;
            }
            return JSON.parse(str);
        }
        let newData = {}
        if (choiceObj.length > 0) {
            choiceObj = JSON.parse(JSON.parse(choiceObj))
            choiceObj.forEach(item => {
                item.answer.forEach((ite, index) => {
                    item.answer[index] = ite.toUpperCase();
                })
            })
            choiceObj = JSON.stringify(choiceObj)
            newData.choice = choiceObj
        }
        if (answerObj.length > 0) newData.answer = answerObj
        if (judgeObj.length > 0) newData.judge = judgeObj
        if (conpletionObj.length > 0) newData.conpletion = conpletionObj
        newData.handpapertime = getTime()
        newData.testpaperId = checkedId.data
        newData.name = checkedName.data
        newData.testnumber = checkedTest.data
        let isTestNumber = await achievement.findOne({ where: { testnumber }, attributes: ["testpaper_id", "id"] })
        if (isTestNumber) {
            // 如果该用户已有此试卷 则更新该试卷
            if (isTestNumber.dataValues.testpaper_id == checkedId.data) {
                let isUpdate = await achievement.update(newData, { where: { id: isTestNumber.id } })
                if (isUpdate) return { msg: "更新成绩成功", code: "200" }
                return { msg: "更新成绩失败", code: "400" }
            } else {
                // 若该用户没有此试卷 则添加成绩
                let res = await achievement.create(newData)
                if (res) return { msg: "添加成绩成功", code: "200" }
                return { msg: "添加成绩失败", code: "400" }
            }
        } else {
            let res = await achievement.create(newData)
            if (res) return { msg: "添加成绩成功", code: "200" }
            return { msg: "添加成绩失败", code: "400" }
        }
    } catch (err) { return { msg: "添加成绩失败", code: "400" } }
}
// 编辑成绩
exports.edit = async ({ id, updator, choice, answer, judge, conpletion, testpaper_id, testnumber, name }) => {
    try {
        let checkedId = checkNull(id, "成绩id")
        if (checkedId.code !== "200") return checkedId
        return { id, updator, choice, answer, judge, conpletion, testpaper_id, testnumber, name }
    } catch (err) { return { msg: "成绩失败", code: "400" } }
}
// 删除成绩
exports.remove = async ({ id }) => {
    try {
        let checkedId = checkNull(id, "成绩id")
        if (checkedId.cde !== "200") return checkedId
    } catch (err) { return { msg: "成绩失败", code: "400" } }
}